// pages/productList/productListPage.js
var httpRequest = require("../../utils/httpRequest.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currIndex:1,
    pageSize:10,
    countLength:0,
    beefData:[],
    textPart:{
      width:100,
    },
    textOriginalPlace:{
      width:100,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx.getSystemInfoSync().windowWidth);

    var urlRequest = "http://ptit.azure-api.cn/v1/products/" + this.data.currIndex + "/" + this.data.pageSize;
    var paramData = {};
    httpRequest.get(urlRequest, paramData).then(res => {
      this.setData({
        countLength: res.data.productLength,
        beefData: res.data.productList
      });
      this.setData({
        textPart: {
          width: 200,
        },
      })
      console.log(this.data.beefData);
    }).catch(err => {
    });

  },

  

  swipToDetail:function(){
    wx.navigateTo({
      url: "../productDetail/productDetailPage"
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.data.currIndex = 1;
    var urlRequest = "http://ptit.azure-api.cn/v1/products/" + this.data.currIndex + "/" + this.data.pageSize;
    var paramData = {};
    httpRequest.get(urlRequest, paramData).then(res => {
      this.setData({
        countLength:res.data.productLength,
        beefData: res.data.productList
      });
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      console.log(this.data.beefData);
    }).catch(err => {
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.beefData.length >= this.data.countLength){
      wx.showToast({
        title: "没有更多数据啦",
        icon:'nomore',
        duration:2000
      });
      return;
    }

    wx.showLoading({
      title:"玩命加载中",
    })
    this.data.currIndex++;
    var urlRequest = "http://ptit.azure-api.cn/v1/products/" + this.data.currIndex + "/" + this.data.pageSize;
    var paramData = {};
    httpRequest.get(urlRequest, paramData).then(res => {
      var temp_list = this.data.beefData;
      for(var i=0; i<res.data.productList.length; i++){
        temp_list.push(res.data.productList[i]);
      }
      this.setData({
        countLength: res.data.productLength,
        beefData: temp_list
      });
      wx.hideLoading();
    }).catch(err => {
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})